import pandas as pd
from flask import Flask, render_template
from static import settings

# ---------- 数据清洗与处理 ----------
f = pd.read_csv('recruit_data.csv')
bigData = f[f['post'].str.contains('大数据')]
citys = list(bigData['city'].value_counts().index)
citys = list(set([i.split('-')[0] for i in citys if len(i.split('-')[0]) < 3]))
ans = []
for city in citys:
    hasCity=bigData['city'].str.contains(city).fillna(False)
    demand=len(bigData[hasCity]['city'])
    minMeanWages=int(bigData[hasCity]['min_wages'].apply(lambda x: float(x)).mean())
    maxMeanWages=int(bigData[hasCity]['max_wages'].apply(lambda x: float(x)).mean())
    ans.append({'name': city, 'demand': demand, 'minMeanWages': minMeanWages, 'maxMeanWages': maxMeanWages})


# 使用快排对以demand为key进行dic的排序

def fastSort(ls):
    left = []
    right = []
    if not ls:
        return []
    for dic in ls[:-1]:
        if dic['demand'] > ls[-1]['demand']:
            left.append(dic)
        else:
            right.append(dic)
    return fastSort(left) + [ls[-1]] + fastSort(right)


data = fastSort(ans)[:8]  # 取前八名
# print(data)

# ---------- Flask 路由 ----------

app = Flask(__name__)
app.config.from_object(settings)


@app.route('/')
def index():
    return render_template('ksh3.html', data=data)


if __name__ == '__main__':
    app.run()
